﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GE_Database_Manager
{
    public class ErrorLog
    {
        // keep track of file IO
        private String dir;
        DateTime time;
        String fileName;

        // constructor
        public ErrorLog(){

            // set the directory name
            dir = "Error Log";

            // create directory if it dosent exists
            if (!Directory.Exists(dir)) {
                Directory.CreateDirectory(dir);
            }

            // grab the date and time
            time = DateTime.Now;

            // set the file name
            // replace invalid characters and spaces
            fileName = time.ToString().Replace('/', '-').Replace(':', '-').Replace(' ', '_');
        }

        /// <summary>
        /// Function to write message to log,
        /// will need the database it comes from
        /// </summary>
        /// <param name="databse"></param>
        /// <param name="Message"></param>
        public void writeToLog(String database,String Message){

            using ( StreamWriter file = getLogFile() )
            {
                file.WriteLine(String.Format("{0}--[{1}]--\"{2}\" ", database, DateTime.Now, Message));
            }
            
        }

        private StreamWriter getLogFile() {
            return new StreamWriter(String.Format("{0}\\{1}.txt", dir, fileName));
        }

    }
}
